﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DGSFramework;
using System.Web.Security;

namespace Framework.WebmasterTool.Comments
{
    public partial class VideoEdit : System.Web.UI.Page
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CheckPermission();
                LoadDetailsVideoComment();
            }
        }

        public void CheckPermission()
        {
            MembershipUser someUser = Membership.GetUser();
            if (someUser == null)
            {
                Response.Redirect(Globals.ApplicationPath + "LoginAdmin/Default.aspx");
            }
            bool flag = false;
            var user = entity.aspnet_Users.Where(us => us.UserName == someUser.UserName).FirstOrDefault();
            user.aspnet_Roles.Load();
            var rolesForUser = user.aspnet_Roles.ToList();
            foreach (var item in rolesForUser)
            {
                var permission = entity.udv_Permissions.Where(ps => ps.RoleId == item.RoleId).FirstOrDefault();
                if (permission.Comment.Value == true)
                {
                    flag = true;
                }
            }
            if (flag == false)
            {
                lblPermissionMessage.Visible = true;
                Panel1.Visible = false;
            }
        }

        public void LoadDetailsVideoComment()
        {
            if (Request.QueryString["CommentID"] != null)
            {
                int commentID = 0;
                int.TryParse(Request.QueryString["CommentID"].ToString(), out commentID);
                var commentVideo = entity.udv_VideoComments.Where(vc => vc.CommentID == commentID).FirstOrDefault();
                if (commentVideo != null)
                {
                    if (commentVideo.Levels == 0)
                    {
                        rdbLevel0.Checked = true;
                    }
                    else if (commentVideo.Levels == 1)
                    {
                        rdbLevel1.Checked = true;
                    }
                    else if (commentVideo.Levels == 2)
                    {
                        rdbLevel2.Checked = true;
                    }
                    else if (commentVideo.Levels == 3)
                    {
                        rdbLevel3.Checked = true;
                    }
                    else if (commentVideo.Levels == 4)
                    {
                        rdbLevel4.Checked = true;
                    }
                    else
                    {
                        rdbLevel5.Checked = true;
                    }
                    txtComment.Text = commentVideo.Comment;
                    commentVideo.udv_VideosReference.Load();
                    hplVideoTitle.NavigateUrl = Globals.ApplicationPath + "WebmasterTool/VideoClips.aspx?ClipID=" + commentVideo.udv_Videos.VideoID;
                    hplVideoTitle.Text = commentVideo.udv_Videos.Title.Trim();
                    commentVideo.aspnet_UsersReference.Load();
                    if (commentVideo.aspnet_Users != null)
                    {
                        lblAddedBy.Text = commentVideo.aspnet_Users.UserName;
                    }
                    else
                    {
                        lblAddedBy.Text = commentVideo.AddedBy;
                    }
                    lblEmail.Text = commentVideo.Title;
                    lblAddedDate.Text = String.Format("{0:dd/MM/yyyy HH:mm:ss tt}", commentVideo.AddedDate);
                    if (commentVideo.UpdatedBy != null)
                    {
                        lblUpdatedBy.Text = commentVideo.UpdatedBy;
                    }
                    lblUpdatedDate.Text = String.Format("{0:dd/MM/yyyy HH:mm:ss tt}", commentVideo.UpdatedDate);
                    chkIsActive.Checked = commentVideo.IsActive.Value;
                }
                else
                {
                    Response.Redirect("Videos.aspx");
                }
            }
            else
            {
                Response.Redirect("Videos.aspx");
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Videos.aspx");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["CommentID"] != null)
            {
                int commentID = 0;
                int.TryParse(Request.QueryString["CommentID"].ToString(), out commentID);
                var commentVideo = entity.udv_VideoComments.Where(vc => vc.CommentID == commentID).FirstOrDefault();
                if (commentVideo != null)
                {
                    if (rdbLevel0.Checked == true)
                    {
                        commentVideo.Levels = 0;
                    }
                    else if (rdbLevel1.Checked == true)
                    {
                        commentVideo.Levels = 1;
                    }
                    else if (rdbLevel2.Checked == true)
                    {
                        commentVideo.Levels = 2;
                    }
                    else if (rdbLevel3.Checked == true)
                    {
                        commentVideo.Levels = 3;
                    }
                    else if (rdbLevel4.Checked == true)
                    {
                        commentVideo.Levels = 4;
                    }
                    else
                    {
                        commentVideo.Levels = 5;
                    }
                    string oldTitle = commentVideo.Comment;
                    commentVideo.Comment = txtComment.Text.Trim();
                    commentVideo.UpdatedBy = Membership.GetUser().UserName;
                    commentVideo.UpdatedDate = DateTime.Now;
                    commentVideo.IsActive = chkIsActive.Checked;
                    entity.SaveChanges();
                    //create db log
                    commentVideo.udv_VideosReference.Load();
                    CreateDBLog(null, null, TypeAction.Update, "Cập nhật bình luận video clip: " + commentVideo.udv_Videos.Title, "", "(" + oldTitle + ") - (" + commentVideo.Comment + ")", "", "",
                            commentVideo.IsActive.Value.ToString(), "", "", "");
                    //data cache
                    Cache.Remove("CommentVideo");
                    Response.Redirect("Videos.aspx");
                }
                else
                {
                    Response.Redirect("Videos.aspx");
                }
            }
            else
            {
                Response.Redirect("Videos.aspx");
            }
        }

        public void CreateDBLog(int? postID, int? postCateID, TypeAction typeAction, string title, string _abstract, string description,
       string releaseDate, string expireDate, string isApproved, string statusID, string metaDescription, string metaKeywords)
        {
            udv_Log log = new udv_Log();
            log.PostID = postID;
            log.PostCateID = postCateID;
            log.TypeAction = (int)typeAction;
            log.Title = title;
            log.Abstract = _abstract;
            log.Description = description;
            log.ReleaseDate = releaseDate;
            log.ExpireDate = expireDate;
            log.IsApproved = isApproved;
            log.StatusID = statusID;
            log.MetaDescription = metaDescription;
            log.MetaKeywords = metaKeywords;
            log.ActionedDate = DateTime.Now;
            log.ActionBy = Membership.GetUser().UserName;
            log.IpAdress = Globals.GetClientIPAddress();

            entity.AddToudv_Log(log);
            entity.SaveChanges();
        }
    }
}